﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;

namespace FGUFW.Core.System
{
	public class UISystem : MonoBehaviour
	{
		static public class Config
		{
			static public readonly Vector3 HIDE_POS = new Vector3(10000,0,0);
			static public readonly Vector3 SHOW_POS = Vector3.zero;
		}
		public Transform ViewRoot;
		public string ResRoot;

		public void Open(string viewName,Action<GameObject> completed)
		{
			Transform view=null;
			foreach (Transform item in ViewRoot)
			{
				if(item.name==viewName && !view.gameObject.activeSelf)
				{
					view = item;
				}
			}

			if(view==null)
			{
				string viewFullPath = ResRoot+viewName+".prefab";
				Addressables.InstantiateAsync(viewFullPath,ViewRoot).Completed+=(handel)=>
				{
					view = handel.Result.transform;
					view.name=viewName;
					view.SetSiblingIndex(ViewRoot.childCount-1);
					completed?.Invoke(view.gameObject);
				};
			}
			else
			{
				view.SetSiblingIndex(ViewRoot.childCount-1);
				completed?.Invoke(view.gameObject);
			}
		}
	}
}
